<template>
  <el-dialog :title="title" :visible.sync="openDialog" @close="close">
    <el-row type="flex" justify="center">
      <el-col :span="18">
        <el-form ref="addform" :model="form" label-suffix="：" :rules="rules" label-position="right" label-width="120px">
          <!-- <el-form-item label="上级分类" v-if="form.level>1">
            <el-input v-model="form.name" disabled autocomplete="off" />
          </el-form-item> -->
          <el-form-item label="上级分类id" v-show="form.level > 1">
            <el-input v-model="form.pid" disabled autocomplete="off" />
          </el-form-item>
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="是否有效">
            <el-switch v-model="form.del" :active-value="2" :inactive-value="1" active-text="是" inactive-text="否"></el-switch>
          </el-form-item>
          <el-form-item label="分类图标" prop="img">
            <pt-upload  v-model="form.img" :width='74' :height="74"></pt-upload>
          </el-form-item>
          <el-form-item label="关键词" prop="keyword">
            <el-input v-model="form.keyword" autocomplete="off" />
          </el-form-item>
          <el-form-item label="分类描述" prop="description">
            <el-input v-model="form.description" />
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <el-button @click="openDialog = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      id: '',
      title: '新增分类信息',
      openDialog: false,
      form: {
        pid: '',
        name: '',
        del: 2,
        img: '',
        keyword: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ],
        img: [{ required: true, message: '请上传分类缩略图', trigger: 'change' }],
        description: [{min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'change'}]
      }
    }
  },
  computed: {
    queryData() {
      let obj = this.form
      obj.id = this.id
      return obj
    }
  },
  methods: {
    init(val, type) {
      if (val) {
        if (type === 0) {
          //新增下级分类
          this.title = '新增下级分类信息'
          this.form = this.$options.data().form
          this.form.pid = val.id
          this.id = ''
        } else {
          this.title = '修改分类信息'
          this.id = val.id
          this.form = JSON.parse(JSON.stringify(val))
        }
      } else {
        this.title = '新增分类信息'
        this.id = ''
      }
      this.openDialog = true
    },
    close() {
      this.$refs.addform.resetFields()
      this.form = this.$options.data().form
    },
    save() {
      this.$refs.addform.validate(valid => {
        if (valid) {
          this.$http.httpPost('/cate/save', this.queryData).then(res => {
            console.log(res)
            if (res) {
              this.$emit('save', '提交')
              this.openDialog = false
              this.$operateTip()
            }
          }).catch(re=>{
            this.$message.error(re)
          })
        }
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 148px;
    height: 148px;
    display: block;
  }
</style>


<style lang="less" scoped>


</style>
